package timgmbh.monopoly.modules;

import timgmbh.monopoly.general.Config;
import timgmbh.monopoly.services.ParserService;
import timgmbh.monopoly.services.SessionService;

public class Home extends BaseModule {	
	public void execute() {
		
		if (SessionService.getUser() != null) {
			//Sets HTML-Titel to welcome
			ParserService.setTitle("Willkommen!");
			//Gives User data to HTML-document
			ParserService.addContent("<center><img width=\"100px\" src=\"" + Config.getGfxPath() + "user.png\"/><br>" + SessionService.getUser().getUsername());
			//States whether the user has admin-rights
			if (SessionService.isAdmin()) ParserService.addContent(" (Administrator)</center>");
			
			ParserService.addContent("<br><br><center><a class=\"classname\" href=\"?site=gamelounge\">Zur Gamelounge</a></br></center>");
		} else {
			if (ParserService.request.getMethod() == "POST") {
				//Declaration of variables
				String username;
				String password;
				String error = "";
				
				try {
					//Initializes username and password
					username = ParserService.request.getParameter("login").toString();
					password = ParserService.request.getParameter("password").toString();
				
					//Prints message if the login was unsuccessful (username and password not matching)
					if (SessionService.login(username, password) == false) error = "Die Anmeldung ist fehlgeschlagen. <a href=\"?site=home\">zurück</a>";
				} catch (Exception e) {
					//Initializes error message
					error = "Du konntest leider nicht eingeloggt werden";
				}
				
				//starts if-clause if no error was found previously
				if (error.length() == 0) {					
					try {
						//Redirection to INDEX.jsp
						ParserService.response.sendRedirect(Config.URL + "/index.jsp");
					} catch (Exception e) {
						//Sends error message to HTML-document
						ParserService.setTitle("Fehler");
						ParserService.addContent(error);
					}
				} else {
					//Sends error message to HTML-document
					ParserService.setTitle("Fehler");
					ParserService.addContent(error);
				}
			} else {
				//Relates CSS-document to HTML-document
				ParserService.addHeader("<link rel=\"stylesheet\" href=\"" + Config.getCssPath() + "login.css\">");		
				ParserService.setTitle("Home");
				
				//Gives HTML-content (text, graphics etc.) to HTML-document
				ParserService.addContent(""
					+ "<div class=\"login\">"
					+ "		<h1><img src=\"" + Config.getGfxPath() + "banner.jpg\" /></h1>"
					+ "		<center>"
					+ "			<form method=\"post\" action=\"\">"
					+ "				<p><input type=\"text\" name=\"login\" value=\"\" placeholder=\"Nutzername oder Email-Adresse\"></p>"
					+ "				<p><input type=\"password\" name=\"password\" value=\"\" placeholder=\"Passwort\"></p>"
					+ "				<p class=\"submit\"><input type=\"submit\" name=\"commit\" value=\"Login\"></p>"
					+ "			</form>"
					+ "		</center>"
					+ "</div>"
					+ "<div class=\"login-help\">"
					//+ "		<p>Passwort vergessen? <a href=\"\">Hier zurücksetzen</a></p>"
					+ "		<p>Neu hier? <a href=\"?site=register\">Registriere dich!</a></p>"
					+ "</div>");
			}
		}
	}
}
